package com.xzs.thinking.config;

import com.xzs.thinking.util.TransactionAttributeCreator;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : Iris.Xiao
 * @date : 2017/11/22 14:42
 * @description :
 */
@Aspect
//@Component 事务依然生效
@Configuration
public class TxAdviceInterceptorUAT {

    private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.xzs.thinking..service.*.*(..))";

    @Resource(name="transactionManagerUAT")
    private PlatformTransactionManager transactionManager;

    @Bean("txAdviceAdvisorUat")
    public TransactionInterceptor txAdvice() {
        NameMatchTransactionAttributeSource source = TransactionAttributeCreator.creator();
        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
        return txAdvice;
    }

    @Bean("txAdviceAdvisorUat")
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
        //return new DefaultPointcutAdvisor(pointcut, txAdvice);
    }
}
